import time
import hashlib
import json
import requests
import re
import pymongo

def tb_1688(liveid, qk_1688, num):
    # url = 'https://h5api.m.1688.com/h5/mtop.taobao.powermsg.h5.msg.pulltopicstat/1.0/'
    # url = 'https://h5api.m.taobao.com/h5/mtop.taobao.powermsg.h5.msg.pulltopicstat/1.0/'
    # url = 'https://h5api.m.taobao.com/h5/mtop.taobao.powermsg.msg.pulltopicstat/1.0/'
    # url = 'https://h5api.m.taobao.com/h5/mtop.taobao.powermsg.msg.pulltopicstat/1.0/'
    # ------------------------
    url = 'http://h5api.m.taobao.com/h5/mtop.mediaplatform.live.livedetail/4.0/'

    headers = {
        "Referer": "http://huodong.m.taobao.com/act/talent/live.html",
        "Sec-Fetch-Mode": "no-cors",
        # 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
    }
    # data = {"topic": liveid, "sdkversion":"h5_1.3.7"}
    data = {"liveId": liveid}
    data = str(data)
    APPKEY = '12574478'
    params = {
        'AntiCreep': 'false',
        'jsv': '2.4.0',
        'appKey': APPKEY,
        't': int(time.time() * 1000),
        'sign': 'FAKE_SIGN_WITH_ANYTHING',
        'api': 'mtop.mediaplatform.live.livedetail',
        'v': '4.0',
        'type': 'json',
        'dataType': 'json',
        'callback': 'mtopjsonp{}'.format(num),
        'data': data
    }
    headers['Cookie'] = qk_1688
    token_with_time = re.findall('_m_h5_tk=(.*?);', qk_1688)[0]
    token = token_with_time.split('_')[0]
    t2 = str(int(time.time() * 1000))
    c = '&'.join([token, t2, APPKEY, data])
    m = hashlib.md5()
    m.update(c.encode('utf-8'))
    params.update({'t': t2, 'sign': m.hexdigest()})

    r2 = requests.get(url, params=params, headers=headers)
    # return json.loads(r2.text)['data']
    # print(params)
    print(r2.text)
    return json.loads(r2.text)



def daren(qk_1688):
    for i in range(3):
        try:
            url = 'https://h5api.m.taobao.com/h5/mtop.taobao.daren.accountpage.componentization.render/1.0/'

            headers = {
                "Referer": "https://daren.taobao.com/account_page/daren_home.htm?wh_weex=true&user_id=1998380794&userId=1998380794",
                "Sec-Fetch-Mode": "no-cors",
                'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
            }
            # data = {"topic": liveid, "sdkversion":"h5_1.3.7"}
            data = {"darenId": "1998380794", "components": "loft,appbar,headerKol,tools,tab,recommend,groupChat,living,feeds,footerMenu"}
            data = str(data)
            APPKEY = '12574478'
            params = {
                'AntiCreep': 'false',
                'jsv': '2.4.3',
                'appKey': APPKEY,
                't': int(time.time() * 1000),
                'sign': 'FAKE_SIGN_WITH_ANYTHING',
                'api': 'mtop.taobao.daren.accountpage.componentization.render',
                'v': '1.0',
                'type': 'json',
                'dataType': 'json',
                'callback': 'mtopjsonp1',
                'data': data
            }
            headers['Cookie'] = qk_1688
            token_with_time = re.findall('_m_h5_tk=(.*?);', qk_1688)[0]
            token = token_with_time.split('_')[0]
            t2 = str(int(time.time() * 1000))
            c = '&'.join([token, t2, APPKEY, data])
            m = hashlib.md5()
            m.update(c.encode('utf-8'))
            params.update({'t': t2, 'sign': m.hexdigest()})

            r2 = requests.get(url, params=params, headers=headers)
            # return json.loads(r2.text)['data']
            # print(params)
            print(r2)
            print(r2.text)
            return json.loads(r2.text)
            break
        except:
            continue



if __name__ == '__main__':

    # qk_1688 = "t=9fd98146f226777e0a50d7745862d5a2; cookie2=18cfa44f4f1bf571e2e8ba22d4e7f60d; v=0; _tb_token_=f787eb10b37fe; alimamapwag=TW96aWxsYS80LjAgKGNvbXBhdGlibGU7IE1TSUUgNy4wOyBXaW5kb3dzIE5UIDYuMjsgV09XNjQ7IFRyaWRlbnQvNy4wOyAuTkVUNC4wQzsgLk5FVDQuMEUp; cookie32=29725a60e4f01d606450e3a8e9187b13; alimamapw=QyBQEXALQHZyFyEiFCdaQCUNQ1dUVVlRCFVbU2xRBAFXUwICUVMGAQIBV1dQAgZSBlZQB1ICVwVQ%0ABVFdBw%3D%3D; cookie31=NDY5MDYwMDAzLCVFNCVCRCVBMCVFNiU5RCVCMCVFOSU5NCU4NSU0MGFsaW1hbWEsODczMTg1Nzk3QGFsaW1hbWEuY29tLFRC; login=WqG3DMC9VAQiUQ%3D%3D"
    cookie = "_m_h5_tk=f20963e373f04a14aef7d75b70a0117b_1571044335697;_m_h5_tk_enc=c4a6d306ca668f6633ab3aee1e638b2f"

    join_count_sum = []

    for i in range(0, 20):
        res = tb_1688("238026969879", cookie, i)
        time.sleep(2)

    print(sum(join_count_sum))
    print(join_count_sum)

    # print(daren(qk_1688))